# -*- coding:utf-8 -*-
import datetime
import os
import smtplib
from email.header import Header
from email.mime.image import MIMEImage

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from Base import GetYamlFile

#第三方SMTP服务
from GetBugResults import get_bug_by_template_result
from templateTest import template_test

mail_host ="smtp.tpson.cn" #设置服务器
mail_user= "linzhou@tpson.cn" #用户名
mail_pass= "Lz1013143" #
sender ="linzhou@tpson.cn" #发送邮箱号
receivers ='research@dingsafe.com' #接收邮箱的邮箱号
today = datetime.datetime.now().strftime('%Y-%m-%d')
SubjectContent=today+"软件项目bug&延期bug统计"
attname=SubjectContent+'.xlsx' 
def sendmail(body,expiredBug):
        YmlList = GetYamlFile.getYamlFile()
        Bug = YmlList["BugPath"]
        PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
        REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, Bug)
        message= MIMEMultipart()
        message['from']=Header(sender,'utf-8')
        message['to'] =Header(receivers,'utf-8')
        message['Subject']=Header(SubjectContent)
        message.attach(MIMEText(SubjectContent,'plain','utf-8'))
        title = datetime.datetime.now().strftime('%Y-%m-%d')
        html=template_test(title, body,expiredBug)
        htm = MIMEText(html, 'html', 'utf-8')
        message.attach(htm)
        # 构造图片

        fp = open('/home/work/GetBugByChanDao/Test/test1.jpg', 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<image1>')
        message.attach(msgImage)
        # att1 = MIMEText(open(REPORT_XLSX_DIR, 'rb').read(),'base64','utf-8')
        # att1["Content-Type"] = 'application/octet-stream'
        # # 这里的filename可以任意写，写什么名字，邮件中显示什么名字
        # att1.add_header('Content-Disposition', 'attachment', filename=('gbk', '', attname) )
        # # print(attname)
        # message.attach(att1)

        try:
            server=smtplib.SMTP_SSL(mail_host)
            server.login(mail_user,mail_pass)
            server.sendmail(sender,receivers,message.as_string())
            server.quit()
            print("邮件发送成功")
        except:
            print("邮件发送失败")

if __name__ == '__main__':
    list1=get_bug_by_template_result()
    expiredBug =[{'name': '消防智能监控系统', 'bugId': '12856', 'bugDescribe': '老消控平台10.2日 设备中心挂了，mq崩了，原因排查', 'bugByP': '何牡禄', 'delaytime': '2021-10-15'}, {'name': '消防智能监控系统', 'bugId': '12813', 'bugDescribe': '晨脉系统老消控重新部署-linux系统', 'bugByP': '何牡禄', 'delaytime': '2021-10-11'}, {'name': '消防智能监控系统', 'bugId': '12628', 'bugDescribe': '老消控-报警邮件推送&报表邮件推送bug', 'bugByP': '何牡禄', 'delaytime': '2021-09-10'}, {'name': '消防智能监控系统', 'bugId': '12588', 'bugDescribe': '晨脉本地部署项目-SMR3250编辑添加负责人报错', 'bugByP': '王佳辉', 'delaytime': '2021-09-06'}, {'name': '消防智能监控系统', 'bugId': '12349', 'bugDescribe': '千里眼项目iosapp报警无弹窗，无推送', 'bugByP': '何牡禄', 'delaytime': '2021-08-16'}, {'name': '消防智能监控系统', 'bugId': '12196', 'bugDescribe': '拓普索尔用水-配置项提示错误，限制也在0-10kpa 可能是0-10mpa，提示文案应该大1000倍', 'bugByP': '厉江涛', 'delaytime': '2021-07-23'}, {'name': '渠道大平台', 'bugId': '12860', 'bugDescribe': 'SMR3002-EX/EF新增导入设备-子设备模式无法添加网关无法正常导入', 'bugByP': '朱圣刚', 'delaytime': '2021-10-13'}, {'name': '渠道大平台', 'bugId': '12859', 'bugDescribe': 'GW100-子设备SMR3002传感器显示数字3 应有一个名称', 'bugByP': '朱圣刚', 'delaytime': '2021-10-13'}, {'name': '渠道大平台', 'bugId': '12583', 'bugDescribe': '批量配置，没有根据账号默认展示的配置来展示数据，而是全部展示可配置', 'bugByP': '王佳辉', 'delaytime': '2021-09-06'}, {'name': '渠道大平台', 'bugId': '12010', 'bugDescribe': '电器管理-电器监管-主机列表未按照页数正常展示', 'bugByP': '沈湘君', 'delaytime': '2021-05-14'}, {'name': '渠道大平台', 'bugId': '11688', 'bugDescribe': '电信设备导出后，表格中无电信模型展示', 'bugByP': '沈湘君', 'delaytime': '2021-04-13'}, {'name': '消防后勤系统', 'bugId': '12601', 'bugDescribe': '车辆管理，车辆列表维修中车辆无统计数据             /logisticsApi/vehicle/statistics', 'bugByP': '周清江', 'delaytime': '2021-09-06'}, {'name': '数字用电', 'bugId': '12847', 'bugDescribe': '查询已完成工单时图片返回为空', 'bugByP': '詹佳敏', 'delaytime': '2021-10-13'}, {'name': '数字用电', 'bugId': '12547', 'bugDescribe': '首页数据总览报警数据显示优化', 'bugByP': '叶佳敏', 'delaytime': '2021-09-03'}, {'name': '数字用电', 'bugId': '12432', 'bugDescribe': '创建和编辑企业时可注册重复的邮箱', 'bugByP': '叶佳敏', 'delaytime': '2020-07-21'}]
    sendmail(list1,expiredBug)